#include "put_image.h"

Put_Image::Put_Image(datebase *db,FileManage *filemg,QObject *parent) :
    QThread(parent)
{
    this->myDb=db;
    this->filelink=filemg;
}

void Put_Image::put_image()//上传照片
{
    ticketCheck_t checkInfo;
    bool res;
    /*res=this->myDb->inOutimage_selcet(&this->myDb->sqlite_db,&checkInfo);
    if(res)
    {
        if(checkInfo.dirflag=="LOCAL")
        {
            QByteArray mybaty= filelink->openMyFile(1,checkInfo.ticketReturn);
             tempcarimage(mybaty,checkInfo.ticketReturn);
        }else{
            QByteArray mybaty= filelink->openMyFile(2,checkInfo.ticketReturn);
            tempcarimage(mybaty,checkInfo.ticketReturn);
        }
    }else{
        emit PutSignal(false);
     }*/
    res=this->myDb->inOutimage_selcet(&this->myDb->sqlite_db,&checkInfo);
    if(res)
    {
        QByteArray mybaty= filelink->openMyDateFile(checkInfo.dirflag,checkInfo.ticketReturn);
        if(mybaty.isEmpty())
        {
        this->myDb->inOutimage_move(&this->myDb->sqlite_db,checkInfo.ticketReturn);
        emit PutSignal(true);
        }else{
        tempcarimage(mybaty,checkInfo.ticketReturn);
        }
    }else{
        emit PutSignal(false);
    }
}


void Put_Image::tempcarimage(QByteArray image,QString file_name)
{
    QString str;
    QEventLoop loop;
    QNetworkAccessManager manager;
    //=("http://192.168.10.78:30/api/v1/hand_open_out");
    str="http://";
    str=str.append(sysInfo->serverIp).append("/api/v1/upload_image");
    QByteArray my_filename=file_name.toAscii();
    QByteArray bydata;
    bydata.append("controller_sn=").append(sysInfo->galleryNumber).append("&file=").append(image).append("&file_name=").append(my_filename);
    QUrl url(str);
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    request.setHeader(QNetworkRequest::ContentLengthHeader, bydata.length());
    QNetworkReply *reply=manager.post(request,bydata);
    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();
    generalWebTempImage(reply);
}

bool Put_Image::generalWebTempImage(QNetworkReply *reply)
{
    QVariant statusCodeV =reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    QVariant redirectionTargetUrl =reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    qDebug()<<"TempImage"<<statusCodeV.toString ()<<redirectionTargetUrl.toString ();
    if (reply->error() == QNetworkReply::NoError)
    {
        QTextCodec *codec = QTextCodec::codecForName("UTF-8");
        QString str = codec->toUnicode(reply->readAll());
        qDebug()<<"*****PutTempImage*****"<<str;
        QScriptEngine engine;
        QScriptValue sc = engine.evaluate("value="+str);
        QString Ref=sc.property("ref").toString ();
        QString Mes=sc.property("msg").toString ();
        if(Ref==QString::number(1))
        {
            reply->deleteLater();
            emit PutSignal(false);
            return false;
        }else if(Ref==QString::number(0)){

            QString res;
            res=this->myDb->hand_InImage(&this->myDb->sqlite_db,Mes);
            if(res!="null")
            {
                this->myDb->inOutimage_move(&this->myDb->sqlite_db,Mes);
                filelink->MoveMyFile(res,Mes);
                emit PutSignal(true);
            }

           reply->deleteLater();
           emit PutSignal(true);
           return true;
        }else{
           emit PutSignal(false);
           return false;
        }
    }else{
     emit PutSignal(false);
     return false;
    }
}

void Put_Image::run()
{
    put_image();
}
